<?php

class IndexController extends Zend_Controller_Action
{

    /**
     * Prihlaseny uzivatel je presmerovan na playlist
     *
     * Neprihlaseny jde na login
     */
    public function indexAction()
    {
        if (LastFM_User::isActiveUser())
        {
            $this->_forward('index', 'playlist');
        }
        else
        {
            $this->_forward('login', 'user');
        }
    }

    public function isisAction()
    {

        $client = new Zend_Http_Client();

        $client->setConfig(array('strictredirects' => true));

        $client->setUri('http://isis.vse.cz/lide/index.pl');
        $client->setParameterPost('vzorek', $_GET['xname']);
        $client->setParameterPost('dohledat', 'dohledat');
        $data = $client->request('POST');

        $uri = Zend_Uri_Http::factory($data->getHeader("Location"));

        $id = current($uri->getQueryAsArray("id"));

        $client = new Zend_Http_Client();
        $client->setUri('http://isis.vse.cz/katalog/rozvrhy_view.pl');
        $client->setParameterGet("rozvrh_student", $id);
        if (isset($_GET['lang']))
        {
            $client->setParameterPost('lang', $_GET['lang']);
        }

        $data = $client->request();

        $content = $data->getBody();
        $dom = new DomDocument();

        @$dom->loadHTML($content);

        $xml = simplexml_import_dom($dom);

        $find = $xml->xpath("//body/div/blockquote/div/div/table");

        $table = current($find);


        if (!isset($table->tr[5]))
                {
            echo "<error>User timetable is private</error>";
            }

        $days = array("mon", "tue", "wed", "thu", "fri");


        $hours = array();
        foreach ($table->tr->td as $td)
        {
            $hours[] = (string) $td->b->small;
        }

        $xmlWriter = new XMLWriter();
        $xmlWriter->openUri('php://output');
        $xmlWriter->setIndent(true);

        $xmlWriter->startDocument('1.0', 'UTF-8');
        $xmlWriter->startElement('subjects');
        for ($i = 0; $i < 5; $i++)
        {

            $hour = 0;

            foreach ($table->tr[$i * 2 + 1]->td as $td)
            {
                $hour++;

                if (isset($td->small->a[0]))
                {

                    $xmlWriter->startElement("subject");

                    $subject_hour = ($hour) / 2;

                    $day = $days[$i];

                    $time = $hours[$hour];

                    $type = "unknown";

                    if (((string) $td["class"]) == "rozvrh-pred")
                    {
                        $type = "seminar";
                    }
                    elseif (((string) $td["class"]) == "rozvrh-cvic")
                    {
                        $type = "lecture";
                    }

                    $room = (string) $td->small->a[0];
                    $subject = trim((string) $td->small->a[1]);
                    $teacher = (string) $td->small->i->a[0];


                    $xmlWriter->writeElement("subject", $subject);
                    $xmlWriter->writeElement("hour", $subject_hour);
                    $xmlWriter->writeElement("day", $day);
                    $xmlWriter->writeElement("time", $time);


                    $xmlWriter->writeElement("teacher", $teacher);
                    $xmlWriter->writeElement("room", $room);

                    $xmlWriter->writeElement("class", $type);


                    $xmlWriter->endElement();

                    $hour++;
                }
            }
        }

        $xmlWriter->endElement();
        $xmlWriter->endDocument();

        die;
    }

    /**
     * Pocta nasemu tymu a last.fm + grooveshark
     */
    public function creditsAction()
    {
        $users = array();
        $users[] = 'techi';
        $users[] = 'heavynyah';
        $users[] = 'drvostep';
        $users[] = 'zsiler';
        $users[] = 'heretic_cz';

        $authors = array();

        foreach ($users as $user_name)
        {
            $user = new LastFM_User($user_name);
            $authors[] = $user->getInfo();
        }

        $this->view->authors = $authors;
        $this->view->headTitle('Credits');
    }

    public function faqAction()
    {
        
    }

    /**
     * Zmeni jazyk
     *
     * @todo centralne kontrolovat povolene hodnoty
     */
    public function selectLanguageAction()
    {
        $session = new Zend_Session_Namespace('language');
        $session->language = $this->_getParam('language');
        $this->_helper->redirector->gotoSimple('index', 'index');
    }

}

